#include "stdafx.h"
#include "DoubleHahsMapTest.h"

#define DHMT_STRING_NUM 5000
#define DHMT_STUCT_NUM 100

namespace Nezha
{

	DoubleHashMapTest::~DoubleHashMapTest()
	{
		// to see if it is released.
		int breakPt = 0;
	}

	bool DoubleHashMapTest::initialize()
	{
		setPerUpdateSampler(TestSystem::getInstance().getDefaultSampler());

		mNames4Test.resize(DHMT_STRING_NUM);
		mSt4Test.resize(DHMT_STUCT_NUM);

		TestSystem& ts = TestSystem::getInstance();

		for(u32 i = 0; i < DHMT_STRING_NUM; i++)
		{
			ts.randomizeEnglishString(mNames4Test[i], 32);
		}

		mSteps = 0;

		return NZTest::initialize();

	}

	void DoubleHashMapTest::uninitialize()
	{
		mNames4Test.clear();
		mSt4Test.clear();

		NZTest::uninitialize();
	}

	void DoubleHashMapTest::reset()
	{
		//mMap.clear();
	}

	bool DoubleHashMapTest::updateImpl(float dt)
	{

		++mSteps;

		if(mSteps == 1)
		{
			int n = DHMT_STRING_NUM / 2;

			for(int i = 0; i < n; i++)
			{
				int m = NZRand() % DHMT_STUCT_NUM;
				mMap.insert(mNames4Test[i], mSt4Test[m]);
			}

			TestDHMapSt st;

			for(int i = 0; i < n; i++)
			{
				int m = NZRand() % DHMT_STRING_NUM;
				if(m < n)
				{
					// should found
					if(!mMap.search(mNames4Test[m], st))
					{
						printf("%s should be found, but not found!!\n", mNames4Test[m].c_str());
					}
				}
				else
				{
					if(mMap.search(mNames4Test[m], st))
					{
						printf("%s should be not found, but found??\n", mNames4Test[m].c_str());
					}
				}
			}

			for(int i = 0; i < n; i++)
			{
				mMap.remove(mNames4Test[i]);
			}

			for(int i = 0; i < n; i++)
			{
				if(mMap.search(mNames4Test[n], st))
				{
					printf("%s should be not found, but found??\n", mNames4Test[n].c_str());
				}
			}
		}
		else
		{
			int n = DHMT_STRING_NUM / 2;

			for(int i = 0; i < n; i++)
			{
				int m = NZRand() % DHMT_STUCT_NUM;
				mStdMap.insert(MapPair<_string, TestDHMapSt>(mNames4Test[i], mSt4Test[m]));
			}

			TestDHMapSt st;

			for(int i = 0; i < n; i++)
			{
				int m = NZRand() % DHMT_STRING_NUM;
				StdNameValMap::iterator it = mStdMap.find(mNames4Test[m]);

				if(m < n)
				{
					// should found
					if(it == mStdMap.end())
					{
						printf("%s should be found, but not found!!\n", mNames4Test[m].c_str());
					}
				}
				else
				{
					if(it != mStdMap.end())
					{
						printf("%s should be not found, but found??\n", mNames4Test[m].c_str());
					}
				}
			}

			for(int i = 0; i < n; i++)
			{
				mStdMap.erase(mNames4Test[i]);
			}

			for(int i = 0; i < n; i++)
			{
				StdNameValMap::iterator it = mStdMap.find(mNames4Test[n]);
				if(it != mStdMap.end())
				{
					printf("%s should be not found, but found??\n", mNames4Test[n].c_str());
				}
			}
		}

		return true;
	}



	class DHMTestFactoryAutoRegister
	{
	public:
		DHMTestFactoryAutoRegister()
		{
			TestFactoryTemp<DoubleHashMapTest> * fac = NZ_New TestFactoryTemp<DoubleHashMapTest>("DoubleHashMap");
			TestSystem::getInstance().registerFacoty(static_cast<TestFactory*>(fac));
		}
	};

	static DHMTestFactoryAutoRegister s_DHMTFA;

}//end namespace Nezha